package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /*
    * 套餐的分页查询
    * */
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /*
    * 根据id查询回显套餐信息
    * */
    @GetMapping("/{id}")
    public Result<SetmealVO> getByIdWithFlavor(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getSetmealById(id);
        return Result.success(setmealVO);
    }

    /*
    * 新增套餐以及套餐内的菜品
    * */
    @PostMapping
    @CacheEvict(cacheNames = "setmealCacheByCateId", key = "#setmealDTO.categoryId")
    public Result<String> save(@RequestBody SetmealDTO setmealDTO) {
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /*
    * 修改套餐以及套餐内的菜品
    * */
    @PutMapping
    @CacheEvict(cacheNames = "setmealCacheByCateId",allEntries = true)
    public Result<String> update(@RequestBody SetmealDTO setmealDTO) {
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /*
    * 修改套餐售卖状态
    * */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCacheByCateId",allEntries = true)
    public Result<String> updateStatus(@PathVariable Integer status, Long id) {
        setmealService.UpdateStatus(status, id);
        return Result.success();
    }

    /*
    * 批量删除
    * */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCacheByCateId",allEntries = true)
    public Result<String> deleteByIds(@RequestParam List<Long> ids) {
        setmealService.deleteByIds(ids);
        return Result.success();
    }
}
